package com.cadre.common.utils.oldutil;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/* loaded from: PoiUtil.class */
public class PoiUtil {
    public static Workbook createWorkbook(String templatePath) {
        Workbook workbook;
        try {
            InputStream input = PoiUtil.class.getResourceAsStream(templatePath);
            if (input == null) {
                throw new RuntimeException("templatePath excel file load error :" + templatePath + " , check templatePath file is exists !");
            }
            if (templatePath.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(input);
            } else if (templatePath.endsWith(".xls")) {
                workbook = new HSSFWorkbook(input);
            } else {
                throw new RuntimeException("templatePath file format is not valid , this : " + templatePath + " , eg:.xlsx or xls");
            }
            return workbook;
        } catch (IOException e) {
            throw new RuntimeException("templatePath excel file load error :" + templatePath);
        }
    }

    public static void writeExcel(Workbook wb, String url) {
        try {
            FileOutputStream out = new FileOutputStream(new File(url));
            wb.write(out);
            out.flush();
            ((POIXMLDocument) wb).close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeExcel(Workbook workbook, HttpServletResponse response, String excelName) throws IOException {
        response.reset();
        String dateStr = excelName + "[" + new SimpleDateFormat("yyyy.MM.dd").format(new Date()) + "]";
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + new String(dateStr.getBytes("gbk"), "iso8859-1") + ".xlsx");
        OutputStream output = response.getOutputStream();
        BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
        workbook.write(bufferedOutPut);
        bufferedOutPut.flush();
        bufferedOutPut.close();
        output.close();
    }

    public static void writeExcelFor03(Workbook workbook, HttpServletResponse response, String excelName) throws IOException {
        response.reset();
        String dateStr = excelName + "[" + new SimpleDateFormat("yyyy.MM.dd").format(new Date()) + "]";
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + new String(dateStr.getBytes("gbk"), "iso8859-1") + ".xls");
        OutputStream output = response.getOutputStream();
        BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
        workbook.write(bufferedOutPut);
        bufferedOutPut.flush();
        bufferedOutPut.close();
        output.close();
    }

    public static void copyRowsForCadrList(int startRow, int endRow, int pPosition, Sheet sheet) {
        int pStartRow = startRow - 1;
        int pEndRow = endRow - 1;
        if (!(pStartRow == -1 || pEndRow == -1)) {
            for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
                CellRangeAddress region = sheet.getMergedRegion(i);
                if (region.getFirstRow() >= pStartRow && region.getLastRow() <= pEndRow) {
                    int targetRowFrom = (region.getFirstRow() - pStartRow) + pPosition;
                    int targetRowTo = (region.getLastRow() - pStartRow) + pPosition;
                    CellRangeAddress newRegion = region.copy();
                    newRegion.setFirstRow(targetRowFrom);
                    newRegion.setFirstColumn(region.getFirstColumn());
                    newRegion.setLastRow(targetRowTo);
                    newRegion.setLastColumn(region.getLastColumn());
                    sheet.addMergedRegion(newRegion);
                }
            }
            for (int i2 = pStartRow; i2 <= pEndRow; i2++) {
                Row sourceRow = sheet.getRow(i2);
                int columnCount = sourceRow.getLastCellNum();
                if (sourceRow != null) {
                    Row newRow = sheet.createRow((pPosition - pStartRow) + i2);
                    newRow.setHeight(sourceRow.getHeight());
                    for (int j = 0; j < columnCount; j++) {
                        Cell templateCell = sourceRow.getCell(j);
                        if (templateCell != null) {
                            copyCell(templateCell, newRow.createCell(j));
                        }
                    }
                }
            }
        }
    }

    public static void copyRows(int startRow, int endRow, int pPosition, Sheet sheet) {
        int pStartRow = startRow - 1;
        int pEndRow = endRow - 1;
        if (!(pStartRow == -1 || pEndRow == -1)) {
            for (int i = pStartRow + 1; i <= pEndRow; i++) {
                Row sourceRow = sheet.getRow(i);
                int columnCount = sourceRow.getLastCellNum();
                if (sourceRow != null) {
                    int i2 = (pPosition - pStartRow) + i;
                    Row newRow = sheet.createRow((pPosition - pStartRow) + i);
                    newRow.setHeight(sourceRow.getHeight());
                    for (int j = 0; j < columnCount; j++) {
                        Cell templateCell = sourceRow.getCell(j);
                        if (templateCell != null) {
                            copyCell(templateCell, newRow.createCell(j));
                        }
                    }
                }
            }
        }
    }

    private static void copyCell(Cell srcCell, Cell distCell) {
        distCell.setCellStyle(srcCell.getCellStyle());
        if (srcCell.getCellComment() != null) {
            distCell.setCellComment(srcCell.getCellComment());
        }
        int srcCellType = srcCell.getCellType().getCode();
        distCell.setCellType(srcCell.getCellType());
        if (srcCellType == 0) {
            if (HSSFDateUtil.isCellDateFormatted(srcCell)) {
                distCell.setCellValue(srcCell.getDateCellValue());
            } else {
                distCell.setCellValue(srcCell.getNumericCellValue());
            }
        } else if (srcCellType == 1) {
            distCell.setCellValue(srcCell.getRichStringCellValue());
        } else if (srcCellType != 3) {
            if (srcCellType == 4) {
                distCell.setCellValue(srcCell.getBooleanCellValue());
            } else if (srcCellType == 5) {
                distCell.setCellErrorValue(srcCell.getErrorCellValue());
            } else if (srcCellType == 2) {
                distCell.setCellFormula(srcCell.getCellFormula());
            }
        }
    }

    public static short getNewFrontSize(Sheet sheet, Row row, Cell cc, String str, short fontsize) {
        if (str == null || str.equals("")) {
            return fontsize;
        }
        float width = sheet.getColumnWidthInPixels(cc.getColumnIndex());
        float height = (row.getHeightInPoints() / 72.0f) * 96.0f;
        if (getTextCount(fontsize, width, height) > ((float) str.length())) {
            return fontsize;
        }
        for (short f = fontsize; f >= 4; f = (short) (f - 1)) {
            if (getTextCount(f, width, height) > ((float) str.length())) {
                return f;
            }
            if (f == 4) {
                return f;
            }
        }
        return fontsize;
    }

    private static float getTextCount(short fontsize, float width, float height) {
        float fontpx = (((float) fontsize) / 72.0f) * 96.0f;
        return ((float) ((int) (width / fontpx))) * ((float) ((int) (height / (fontpx + 4.0f))));
    }
}
